package app.fugema.shop.main.invoice.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.base.common.app.LoginUtils;
import com.base.common.model.http.jackSon.JacksonUtils;
import com.base.common.netBeanPackage.BaseApiResult;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.view.base.BaseTitleActivity;
import com.base.common.viewmodel.BaseViewObserver;
import com.jeremyliao.liveeventbus.LiveEventBus;

import androidx.annotation.NonNull;
import app.fugema.shop.R;
import app.fugema.shop.constants.AppConstants;
import app.fugema.shop.databinding.ActivityInvoiceInfoBinding;
import app.fugema.shop.main.invoice.model.InvoiceInfoParams;
import app.fugema.shop.main.invoice.vm.InvoiceVM;

public class InvoiceInfoActivity extends BaseTitleActivity<ActivityInvoiceInfoBinding, InvoiceVM>
{
    // 发票信息

    private boolean isZengZhiTicket = false;

    private boolean isOrgCompany = true;

    private String userKey;

    private  InvoiceInfoParams invoiceInfo;

    public static void start(Context context)
    {
        Intent intent = new Intent(context, InvoiceInfoActivity.class);
        context.startActivity(intent);
    }

    public static void start(Context context,@NonNull  InvoiceInfoParams defInvoice)
    {
        Intent intent = new Intent(context, InvoiceInfoActivity.class);
        intent.putExtra("data", JacksonUtils.transBean2Json(defInvoice));
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invoice_info);
    }

    @Override
    protected int getStatusBarColor()
    {
        return R.color.redColor;
    }

    @Override
    protected void initView()
    {
        super.initView();
        setCloseImg(R.mipmap.ic_back_white);
        setTitle("发票信息",R.color.white);
        //发票类型
        binding.rgTicketType.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId)
            {
                case R.id.rb_e_ticket:
                    //UIUtils.showToastSafes("电子发票");
                    isZengZhiTicket = false;
                    break;
                case R.id.rb_zz_ticket:
                    //UIUtils.showToastSafes("增值税发票");
                    isZengZhiTicket = true;
                    break;
            }
            changeOpenTicketInfo(isZengZhiTicket);
        });
        //
        binding.rgOrgType.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId)
            {
                case R.id.rb_person:
                    //UIUtils.showToastSafes("个人");
                    isOrgCompany = false;
                    break;
                case R.id.rb_company:
                    //UIUtils.showToastSafes("公司");
                    isOrgCompany = true;
                    break;
            }
        });
        //不需要发票
        binding.btnNotTicket.setOnClickListener(v -> {
            //0=不需要发票,1=普通发票，2=电子普通发票，3=增值税发票
            invoiceInfo.setInvoicetype(0);
            LiveEventBus.get(AppConstants.EventKey.INVOICE_SAVE_OK, InvoiceInfoParams.class)
                    .post( invoiceInfo );
            finish();
        });

        //提交保存发票信息 成功后返回 发票信息
        binding.tvOk.setOnClickListener(v -> {

            String name = binding.etCompanyName.getText().toString();
            String code = binding.etShibieCode.getText().toString();
            invoiceInfo.setName( name );
            invoiceInfo.setCode( code );

            String callphone = binding.etReceivePhone.getText().toString();
            invoiceInfo.setCellphone( callphone );

            String mEmail = binding.etReceiveEmail.getText().toString();
            invoiceInfo.setEmail(mEmail);

            invoiceInfo.setUserkey( userKey  );
            //0=不需要发票,1=普通发票，2=电子普通发票，3=增值税发票
            if( isZengZhiTicket  )
            {
                invoiceInfo.setInvoicetype(3);
                if( isOrgCompany )
                {
                    invoiceInfo.setInvoicecontext("公司");
                }
                else
                {
                    invoiceInfo.setInvoicecontext("个人");
                }
            }
            else
            {
               if(isOrgCompany)
               {
                   invoiceInfo.setInvoicetype(2);
                   invoiceInfo.setInvoicecontext("公司");
               }
               else
               {
                   invoiceInfo.setInvoicetype(1);
                   invoiceInfo.setInvoicecontext("个人");
               }
            }

            invoiceInfo.setRegionid("26122");
            viewModel.saveInvoiceTitle( invoiceInfo )
                     .observe(this, new BaseViewObserver<LiveDataWrapper<BaseApiResult>>() {
                         @Override
                         protected void onSuccess(LiveDataWrapper<BaseApiResult> data)
                         {
                             //{"success":true,"msg":"保存成功"}
                             LiveEventBus.get(AppConstants.EventKey.INVOICE_SAVE_OK, InvoiceInfoParams.class)
                                         .post( invoiceInfo );
                             finish();
                         }
                     });
        });
    }

    @Override
    protected void initData(Intent intent)
    {
        super.initData(intent);
        userKey = LoginUtils.getToken();
        // 默认 发票信息
        invoiceInfo =   JacksonUtils.getJsonBean(intent.getStringExtra("data"), InvoiceInfoParams.class);
        binding.setItem(invoiceInfo);
        binding.executePendingBindings();


    }

    private void changeOpenTicketInfo(boolean bIsZengZhiTicket)
    {
        if( bIsZengZhiTicket )
        {
            binding.llZzCompanyInfoView.setVisibility(View.VISIBLE);
            binding.llZzBankInfoView.setVisibility(View.VISIBLE);

            binding.llReciveAdressInfoView.setVisibility(View.VISIBLE);
            binding.llReciveEmailView.setVisibility(View.GONE);
        }
        else
        {
            binding.llZzBankInfoView.setVisibility(View.GONE);

            binding.llReciveAdressInfoView.setVisibility(View.GONE);
            binding.llReciveEmailView.setVisibility(View.VISIBLE);
        }
    }

}
